<template>
  <div class="body">
    <div class="container">
      <div class="title"><span>商家注册</span></div>
      <div class="account">
        <input type="text" placeholder="这里输入账号..." v-model="account" />
      </div>
      <div class="password">
        <input type="text" placeholder="这里输入密码..." v-model="password" />
      </div>
      <div class="phone">
        <input type="text" placeholder="联系方式..." v-model="phone" />
      </div>
      <div class="store_name">
        <input type="text" placeholder="真实姓名..." v-model="storeName" />
      </div>
      <div class="buttons">
        <el-button type="primary" plain class="button" @click="submit"
          >注册</el-button
        >
        <el-button type="success" plain class="button" @click="routeTo('/')"
          >去登录</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { userAxios } from "../common/network/request";

export default {
  data() {
    return {
      account: "",
      password: "",
      phone: "",
      storeName: "",
    };
  },
  methods: {
    routeTo(path) {
      this.$router.push(path);
    },
    submit() {
      if (
        this.account == "" ||
        this.password == "" ||
        this.phone == "" ||
        this.storeName == ""
      ) {
        this.$message({
          type: "error",
          message: "不可为空",
        });
      } else if (!this.isPhone(this.phone)) {
        this.$message({
          type: "error",
          message: "手机号不正确",
        });
      } else {
        let businessUser = {
          account: this.account,
          password: this.password,
          phone: this.phone,
          name: this.storeName,
        };
        userAxios({
          url: "/business/doSignUp",
          method: "post",
          data: businessUser,
        }).then((res) => {
          console.log(res);
          if (res == null || res == "") {
            this.$message({
              type: "error",
              message: "此账号已注册",
            });
          } else {
            this.$message({
              type: "success",
              message: "注册成功",
            });
          }
        });
      }
    },
    isPhone(phone) {
      return /^1[3|4|5|7|8]\d{9}$/.test(Number(phone));
    },
  },
};
</script>

<style scoped>
.body {
  height: 100%;
  background-color: rgb(132, 204, 252);
  display: flex;
  align-items: center;
}

.container {
  height: 500px;
  width: 400px;
  background-color: white;
  border-radius: 19px;
  margin: auto;
  border: 1px solid rgb(224, 220, 220);
  display: flex;
  flex-direction: column;
  padding: 30px;
}

.title {
  flex: 1;
  font-size: 24px;
  color: #606266;
  display: flex;
  align-items: center;
  letter-spacing: 3px;
}

.title span {
  text-align: center;
  margin: auto;
}

.account {
  flex: 1;
  display: flex;
  align-items: center;
}

input {
  outline: none;
  border: none;
  height: 80%;
  width: 80%;
  font-size: 15px;
  font-weight: 300;
  border-bottom: 1px solid gray;
  margin: auto;
  text-align: center;
}

.password {
  flex: 1;
  display: flex;
  align-items: center;
}

.phone {
  flex: 1;
  display: flex;
  align-items: center;
}

.store_name {
  flex: 1;
  display: flex;
  align-items: center;
}

.buttons {
  flex: 2;
  display: flex;
  align-items: center;
  justify-items: center;
  padding-left: 40px;
  padding-right: 40px;
}

.button {
  flex: 1;
}
</style>